<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="救援类型" prop="teamTypeId">
        <el-select v-model="queryParams.teamTypeId" placeholder="救援类型" clearable>
          <el-option
            v-for="dict in dict.type.emg_rescue_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="队名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入队名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="应急水平" prop="emgLevelId">
        <el-select v-model="queryParams.emgLevelId" placeholder="应急水平" clearable>
          <el-option
            v-for="dict in dict.type.emg_team_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="编号" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="救援类型" align="center" prop="teamTypeId" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.emg_rescue_type" :value="scope.row.teamTypeId"/>
        </template>
      </el-table-column>
      <el-table-column label="队名" align="center" prop="name"/>
      <el-table-column label="徽章图片" align="center" prop="badgeUrl" width="200">
        <template slot-scope="scope">
          <ImagePreview  v-if="scope.row.badgeUrl"
            :height=100
            :src="scope.row.badgeUrl"
            :preview-src-list="[scope.row.badgeUrl]"/>
        </template>
      </el-table-column>
      <el-table-column label="队旗图片" align="center" prop="pennantUrl" width="200">
        <template slot-scope="scope">
          <ImagePreview v-if="scope.row.pennantUrl"
            :height=100
            :src="scope.row.pennantUrl"
            :preview-src-list="[scope.row.pennantUrl]"/>
        </template>
      </el-table-column>
      <el-table-column label="队伍合照" align="center" prop="groupPhotoUrl" width="200">
        <template slot-scope="scope">
          <ImagePreview v-if="scope.row.groupPhotoUrl"
            :height=100
            :src="scope.row.groupPhotoUrl"
            :preview-src-list="[scope.row.groupPhotoUrl]"/>
        </template>
      </el-table-column>
      <el-table-column label="规模" align="center" prop="scaleId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.emg_team_scale" :value="scope.row.scaleId"/>
        </template>
      </el-table-column>
      <el-table-column label="应急水平" align="center" prop="emgLevelId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.emg_team_level" :value="scope.row.emgLevelId"/>
        </template>
      </el-table-column>
      <el-table-column label="坐标" align="center" prop="lat"/>
      <el-table-column label="坐标" align="center" prop="lng"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="擅长" align="center" prop="goodAt" show-overflow-tooltip/>
      <el-table-column label="作战经验描述" align="center" prop="combatExperience" show-overflow-tooltip/>
      <el-table-column label="负责人1" align="center" prop="leader1"/>
      <el-table-column label="负责人1电话" align="center" prop="phone1"/>
      <el-table-column label="负责人2" align="center" prop="leader2"/>
      <el-table-column label="负责人2电话" align="center" prop="phone2"/>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改队伍团队对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="救援类型" prop="teamTypeId">
          <el-select v-model="form.teamTypeId" placeholder="救援类型" clearable>
            <el-option
              v-for="dict in dict.type.emg_rescue_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="队名" prop="name">
          <el-input v-model="form.name" placeholder="请输入队名"/>
        </el-form-item>
        <el-form-item label="徽章图片" prop="badgeUrl">
          <singleImageUpload v-model="form.badgeUrl"/>
        </el-form-item>
        <el-form-item label="队旗图片" prop="pennantUrl">
          <singleImageUpload v-model="form.pennantUrl"/>
        </el-form-item>
        <el-form-item label="队伍合照" prop="groupPhotoUrl">
          <singleImageUpload v-model="form.groupPhotoUrl"/>
        </el-form-item>
        <el-form-item label="队伍规模" prop="scaleId">
          <el-select v-model="form.scaleId" placeholder="队伍规模" clearable>
            <el-option
              v-for="dict in dict.type.emg_team_scale"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="应急水平" prop="emgLevelId">
          <el-select v-model="form.emgLevelId" placeholder="应急水平" clearable>
            <el-option
              v-for="dict in dict.type.emg_team_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="坐标" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入坐标"/>
        </el-form-item>
        <el-form-item label="坐标" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入坐标"/>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="擅长" prop="goodAt">
          <el-input type="textarea" v-model="form.goodAt" placeholder="请输入擅长"
                    :autosize="{ minRows: 2, maxRows: 10}"/>
        </el-form-item>
        <el-form-item label="作战经验描述" prop="combatExperience">
          <el-input type="textarea" v-model="form.combatExperience" placeholder="请输入作战经验描述"
                    :autosize="{ minRows: 2, maxRows: 10}"/>
        </el-form-item>
        <el-form-item label="负责人1" prop="leader1">
          <el-input v-model="form.leader1" placeholder="请输入负责人1"/>
        </el-form-item>
        <el-form-item label="负责人1电话" prop="phone1">
          <el-input v-model="form.phone1" placeholder="请输入负责人1电话"/>
        </el-form-item>
        <el-form-item label="负责人2" prop="leader2">
          <el-input v-model="form.leader2" placeholder="请输入负责人2"/>
        </el-form-item>
        <el-form-item label="负责人2电话" prop="phone2">
          <el-input v-model="form.phone2" placeholder="请输入负责人2电话"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTeam, getTeam, delTeam, addTeam, updateTeam} from "@/api/emergency/team";
import {getCustomDicts} from "@/api/system/dict/data";

export default {
  name: "Team",
  dicts: ['sys_normal_disable', 'emg_rescue_type', 'emg_team_scale', 'emg_team_level'],
  data() {
    return {
      imageUrl: '',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 队伍团队表格数据
      teamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamTypeId: undefined,
        name: undefined,
        badgeUrl: undefined,
        pennantUrl: undefined,
        groupPhotoUrl: undefined,
        scaleId: undefined,
        emgLevelId: undefined,
        lat: undefined,
        lng: undefined,
        address: undefined,
        goodAt: undefined,
        combatExperience: undefined,
        leader1: undefined,
        phone1: undefined,
        leader2: undefined,
        phone2: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        teamTypeId: [
          {required: true, message: "救援类型不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "队名不能为空", trigger: "blur"}
        ],
        leader1: [
          {required: true, message: "负责人1不能为空", trigger: "blur"}
        ],
        phone1: [
          {required: true, message: "负责人1电话不能为空", trigger: "blur"}
        ],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询队伍团队列表 */
    getList() {
      this.loading = true;
      listTeam(this.queryParams).then(response => {
        this.teamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        badgeUrl: undefined,
        pennantUrl: undefined,
        groupPhotoUrl: undefined,
        scaleId: undefined,
        emgLevelId: undefined,
        lat: undefined,
        lng: undefined,
        address: undefined,
        goodAt: undefined,
        combatExperience: undefined,
        leader1: undefined,
        phone1: undefined,
        leader2: undefined,
        phone2: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加队伍团队";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getTeam(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改队伍团队";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateTeam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addTeam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除队伍团队编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delTeam(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/team/export', {
        ...this.queryParams
      }, `team_${new Date().getTime()}.xlsx`)
    },
    handleAvatarSuccess(res, file) {
      this.form.badgeUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 5MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
;
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
